package com.sl.au.repository;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventTask;
import com.sl.au.entity.TimeOutReceiveRegister;
import com.sl.au.entity.ViewForTimeOutTask;

public interface TimeoutTaskRepository extends JpaRepository<ViewForTimeOutTask, String> {
	
	/*
	 * 接收超时任务
	 */
	@Query("select distinct b from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 order by b.createTime desc")
	Page<EventRegister> getReceive(String taskStateId, Pageable pageable);

	@Query("select distinct b from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2 order by b.createTime desc")
	Page<EventRegister> getReceiveAndType(String taskStateId, String type, Pageable pageable);

	@Query("select distinct b from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and (b.registerEventName like %?2% or b.registerEventType.eventName like %?2% ) order by b.createTime desc ")
	Page<EventRegister> getReceiveByKey(String taskStateId, String key,Pageable pageable);

	@Query("select distinct b from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2 and (b.registerEventName like %?3% or b.registerEventType.eventName like %?3% ) order by b.createTime desc")
	Page<EventRegister> getReceiveByKeyAndType(String taskStateId, String type ,String key,Pageable pageable);
	
	@Query("select count(distinct b.id) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 ")
	int getReceiveCount(String taskStateId);

	@Query("select count(distinct b.id) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2")
	int getReceiveAndTypeCount(String taskStateId, String type);

	@Query("select count(distinct b.id) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and (b.registerEventName like %?2% or b.registerEventType.eventName like %?2% ) ")
	int getReceiveByKeyCount(String taskStateId, String key);

	@Query("select count(distinct b.id) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.acceptLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2 and (b.registerEventName like %?3% or b.registerEventType.eventName like %?3% ) ")
	int getReceiveByKeyAndTypeCount(String taskStateId, String type, String key);

	/*
	 * 处理超时任务
	 */
	@Query("select new com.sl.au.entity.TimeOutReceiveRegister(a,b) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1")
	Page<TimeOutReceiveRegister> getDeal(String taskStateId, Pageable pageable);

	@Query("select new com.sl.au.entity.TimeOutReceiveRegister(a,b) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2")
	Page<TimeOutReceiveRegister> getDealAndType(String taskStateId, String type,Pageable pageable);
	
	@Query("select new com.sl.au.entity.TimeOutReceiveRegister(a,b) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and (b.registerEventName like %?2% or b.registerEventType.eventName like %?2% ) ")
	Page<TimeOutReceiveRegister> getDealByKey(String taskStateId, String key,Pageable pageable);
	
	@Query("select new com.sl.au.entity.TimeOutReceiveRegister(a,b) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2 and (b.registerEventName like %?3% or b.registerEventType.eventName like %?3% ) ")
	Page<TimeOutReceiveRegister> getDealByKeyAndType(String taskStateId, String type, String key,Pageable pageable);
	
	@Query("select count(*) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 ")
	int getDealCount(String taskStateId);

	@Query("select count(*) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2")
	int getDealAndTypeCount(String taskStateId, String type);
	
	@Query("select count(*) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and (b.registerEventName like %?2% or b.registerEventType.eventName like %?2% ) ")
	int getDealByKeyCount(String taskStateId, String key);

	@Query("select count(*) from ViewForTimeOutTask a, EventRegister b where a.eventId = b.id and b.dealLimitSpan < a.diffTime and a.eventTaskState = ?1 and a.unfinished = ?2 and (b.registerEventName like %?3% or b.registerEventType.eventName like %?3% ) ")
	int getDealByKeyAndTypeCount(String taskStateId, String type, String key);
	
	/*
	 * 任务步骤查询
	 */
	@Query("select e from ViewForTimeOutTask e where e.eventId=?1 order by e.eventTaskTime desc")
	List<ViewForTimeOutTask> findTaskProcedures(String eventId);
}